package com.ivt.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.ivt.core.ex.IvtException;
import com.ivt.core.util.PwdUtils;
import com.ivt.dao.SpecificDao;
import com.ivt.dao.TeacherDao;
import com.ivt.model.Teacher;
import com.ivt.model.view.QueryCriteria;
import com.ivt.service.TeacherService;
import com.ivt.utils.enums.AuditStatus;

@Service
public class TeacherServiceImpl implements TeacherService {

	@Resource
	private TeacherDao teacherDao;

	@Resource
	private SpecificDao specificDao;

	@Override
	public Teacher update(Teacher teacher) throws IvtException {
		if (null == teacher || null == teacher.getId() || 0 >= teacher.getId()) {
			throw new IvtException("学生信息不能为空");
		}

		Teacher original = getTeacher(teacher.getId());

		if (StringUtils.isNotBlank(teacher.getAccount())
				&& 0 < teacherDao.existsByAccountAndId(teacher.getAccount(),
						teacher.getId())) {
			throw new IvtException("公告【" + teacher.getAccount() + "】已存在");
		}
		if (StringUtils.isNotBlank(teacher.getPassword())) {
			teacher.setPassword(PwdUtils.MD256Pws(teacher.getPassword()));
		} else {
			teacher.setPassword(original.getPassword());
		}

		teacher.setStatus(AuditStatus.PASS);
		return teacherDao.saveAndFlush(teacher);
	}

	@Override
	public long queryCount(QueryCriteria queryCriteria) throws IvtException {
		return specificDao.queryStudentCount(queryCriteria);
	}

	@Override
	public List<Teacher> queryByCriteria(QueryCriteria queryCriteria)
			throws IvtException {
		return specificDao.queryTeacher(queryCriteria);
	}

	@Override
	public Teacher delete(Long id) throws IvtException {
		if (null == id || 0 >= id) {
			throw new IvtException("参数错误");
		}

		Teacher t = getTeacher(id);
		t.setStatus(AuditStatus.NOT);
		return teacherDao.saveAndFlush(t);
	}

	@Override
	public int deletes(Long[] ids) throws IvtException {
		if (ArrayUtils.isEmpty(ids)) {
			throw new IvtException("参数错误");
		}

		Set<Long> cleanIds = Sets.newHashSet(ids);
		Iterable<Teacher> teachers = teacherDao.findAll(cleanIds);
		int i = 0;
		if (null != teachers) {
			for (Teacher cz : teachers) {
				cz.setStatus(AuditStatus.NOT);
				teacherDao.saveAndFlush(cz);
				i++;
			}
		}
		return i;
	}

	@Override
	public Teacher getTeacher(Long id) throws IvtException {
		if (null == id || 0 >= id) {
			throw new IvtException("参数错误");
		}
		return teacherDao.findOne(id);
	}

	@Override
	public List<Teacher> getTeachers(Set<Long> ids) throws IvtException {
		if (CollectionUtils.isEmpty(ids)) {
			throw new IvtException("参数错误");
		}

		Iterable<Teacher> coll = teacherDao.findAll(ids);
		return (null == coll) ? null : Lists.newArrayList(coll);
	}

	@Override
	public Teacher create(Teacher teacher) throws IvtException {
		if (null == teacher) {
			throw new IvtException("学生信息不能为空");
		}
		if (StringUtils.isBlank(teacher.getName())) {
			throw new IvtException("姓名不能为空");
		}
		if (StringUtils.isBlank(teacher.getAccount())) {
			throw new IvtException("登入帐号不能为空");
		}

		if (StringUtils.isBlank(teacher.getPassword())) {
			throw new IvtException("登入密码不能为空");
		}
		long c = teacherDao.existsByAccount(teacher.getAccount());
		if (0 < c) {
			throw new IvtException("学生【" + teacher.getAccount() + "】已存在");
		}

		teacher.setPassword(PwdUtils.MD256Pws(teacher.getPassword()));
		teacher.setId(null);
		teacher.setStatus(AuditStatus.PASS);
		return teacherDao.saveAndFlush(teacher);
	}

	@Override
	public Teacher login(String username, String password) throws IvtException {

		if (StringUtils.isBlank(username) || StringUtils.isBlank(username)) {
			throw new IvtException("登陆失败,账号或密码错误");
		}

		List<Teacher> teachers = teacherDao.findByAccount(username);

		if (CollectionUtils.isEmpty(teachers)) {
			throw new IvtException("登陆失败,账号错误");
		}

		Teacher teacher = teachers.get(0);

		if (!PwdUtils.MD256Pws(password).equals(teacher.getPassword())) {
			throw new IvtException("登陆失败,密码错误");
		}

		return teacher;
	}

	protected QueryCriteria cleanQueryCriteria(QueryCriteria queryCriteria) {

		if (null == queryCriteria) {
			return null;
		}
		Map<String, Object> criteria = queryCriteria.splitCriteria();

		if (MapUtils.isNotEmpty(criteria)) {

			if (criteria.containsKey("status")
					&& StringUtils.isNumeric(String.valueOf(criteria
							.get("status")))) {
				AuditStatus status = AuditStatus.findByValue(Integer
						.valueOf(String.valueOf(criteria.get("status"))));
				criteria.put("status", status);
			}
		}

		return queryCriteria;
	}
}
